package com.itheima.service.store.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.ProductDao;
import com.itheima.domain.store.City;
import com.itheima.domain.store.Product;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.ProductService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class ProductServiceImpl implements ProductService {
    @Override
    public void save(Product product) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ProductDao productDao = MapperFactory.getMapper(sqlSession,ProductDao.class);
            //id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString().replace("-","");
            product.setId(id);
            //3.调用Dao层操作
            productDao.save(product);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(Product product) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ProductDao productDao = MapperFactory.getMapper(sqlSession,ProductDao.class);
            //3.调用Dao层操作
            productDao.delete(product);
            System.out.println(product);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Product product) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ProductDao productDao = MapperFactory.getMapper(sqlSession,ProductDao.class);
            //3.调用Dao层操作
            productDao.update(product);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public Product findById(String id) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ProductDao productDao = MapperFactory.getMapper(sqlSession,ProductDao.class);
            //3.调用Dao层操作
            return productDao.findById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

//    @Override
//    public List<Product> findAll() {
//        SqlSession sqlSession = null;
//        try{
//            //1.获取SqlSession
//            sqlSession = MapperFactory.getSqlSession();
//            //2.获取Dao
//            ProductDao productDao = MapperFactory.getMapper(sqlSession,ProductDao.class);
//            //3.调用Dao层操作
//            return productDao.findAll();
//        }catch (Exception e){
//            throw new RuntimeException(e);
//            //记录日志
//        }finally {
//            try {
//                TransactionUtil.close(sqlSession);
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
//    }

    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession = null;
        try{
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ProductDao productDao = MapperFactory.getMapper(sqlSession,ProductDao.class);
            //3.调用Dao层操作
            PageHelper.startPage(page,size);
            List<Product> all = productDao.findAll();
            PageInfo pageInfo = new PageInfo(all);
            return pageInfo;

        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<City> findAllCity() {
        List<City> citys = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过SqlSession工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.获取UserMapper接口的实现类对象
            ProductDao productDao = sqlSession.getMapper(ProductDao.class);
            //5.调用实现类对象的模糊查询方法
            citys = productDao.findAllCity();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            if(sqlSession != null) {
                sqlSession.close();
            }
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //7.返回结果到控制层
        return citys;
    }

}
